//
//  PhotoBrowseController.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/28.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit
/// 照片浏览控制器
class PhotoBrowseController: UIViewController {
   
  private  let selectIndex: Int
    
   private   let urls: [String]
    
//    private let anmiation: PhotoBrowseAnmiation
    
    init(selectIndex: Int,urls: [String]){
        
        self.selectIndex = selectIndex
        
        self.urls = urls
//        //创建动画器
//        anmiation = PhotoBrowseAnmiation()
        
     super.init(nibName: nil, bundle: nil)
        
//        //制定转场动画代理
//        transitioningDelegate = anmiation
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    
    @objc   private func tapClick(){
        
        dismissViewControllerAnimated(true, completion: nil)
    }

}

extension PhotoBrowseController:UIPageViewControllerDataSource{
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        
       var index = (viewController as! PhotoViewController).photoIndex
    
        if index <= 0 {
            
            return nil
        }
        
        index--
        
        return  PhotoViewController(urlString: urls[index], photoIndex: index)
        
        
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        
       var index = (viewController as! PhotoViewController).photoIndex
        
        
        if ++index >= urls.count{
            
            return nil
        }
        
        return PhotoViewController(urlString: urls[index], photoIndex: index)
    }
    
    
}


// MARK: - 设置界面
extension PhotoBrowseController{
   /// 布局界面
    private func setupUI(){
        
    view.backgroundColor = UIColor.blackColor()
        
        //分页控制器
        //1实例化
        let pageVC = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: [UIPageViewControllerOptionInterPageSpacingKey:20])
        let phVc = PhotoViewController(urlString: urls[selectIndex], photoIndex: selectIndex)
        
        pageVC.setViewControllers([phVc], direction: .Forward, animated: false, completion: nil)
        
        addChildViewController(pageVC)
        
        view.addSubview(pageVC.view)
        
        //设置手势识别
        
        view.gestureRecognizers = pageVC.gestureRecognizers
        
        //设置数据源
        
        if urls.count > 1 {
           
            pageVC.dataSource = self
            
        }
        
        
      let tap = UITapGestureRecognizer(target: self, action: "tapClick")
        
        view.addGestureRecognizer(tap)
        
    }
    
}